﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class IntransitDetailBLL
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly IIntransitDetailDA IntransitDetailDA = DataAccess.CreateIntransitDetailDA();
        private PubParasInfo pubParasInfo;

        public IntransitDetailBLL()
        {
            this.pubParasInfo = null;
        }

        public IntransitDetailBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public IntransitDetailBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string DeleteIntransitDetail(List<string> IntransitIDs, List<string> LineIDs)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                int i;
                if (IntransitIDs.Count != LineIDs.Count)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F13500_003", this.pubParasInfo.UserLanguage);
                }
                List<IntransitDetailInfo> intransitDetailInfos = new List<IntransitDetailInfo>();
                IntransitDetailInfo intransitDetailInfo = null;
                for (i = 0; i < IntransitIDs.Count; i++)
                {
                    intransitDetailInfo = IntransitDetailDA.GetIntransitDetailByID(dataBase, tran, IntransitIDs[i], LineIDs[i], this.dataBaseID[0]);
                    intransitDetailInfos.Add(intransitDetailInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if (IntransitDetailDA.DeleteIntransitDetail(dataBase, tran, IntransitIDs, LineIDs, this.dataBaseID) > 0)
                {
                    for (i = 0; i < IntransitIDs.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<IntransitDetailInfo>(dataBase, tran, intransitDetailInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "INTRANSIT_DETAIL", this.dataBaseID[0]);
                        string CS00002 = temp;
                        temp = CS00002 + IntransitIDs[i] + "," + LineIDs[i] + ";";
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            if (temp.Length > 0)
            {
                return temp.Substring(0, temp.Length - 1);
            }
            return "";
        }

        public DataSet GetAllIntransitDetail()
        {
            return IntransitDetailDA.GetAllIntransitDetail(this.dataBaseID[0]);
        }

        public IntransitDetailInfo GetIntransitDetailInfoByID(string IntransitID, string LineID)
        {
            return IntransitDetailDA.GetIntransitDetailByID(IntransitID, LineID, this.dataBaseID[0]);
        }

        public DataSet GetIntransitDetailInfoByQueryEntity(IntransitDetailQueryEntity intransitDetailQueryEntity)
        {
            IntransitDetailQueryEntity intransitDetailQuery = intransitDetailQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(intransitDetailQuery.IntransitID))
            {
                sqlWhere.Add(" INTRANSIT_ID " + intransitDetailQuery.IntransitID);
            }
            if (!string.IsNullOrEmpty(intransitDetailQuery.LineID))
            {
                sqlWhere.Add(" LINE_ID " + intransitDetailQuery.LineID);
            }
            if (!string.IsNullOrEmpty(intransitDetailQuery.OrderLineID))
            {
                sqlWhere.Add(" ORDER_LINE_ID " + intransitDetailQuery.OrderLineID);
            }
            if (!string.IsNullOrEmpty(intransitDetailQuery.ReceiptLineID))
            {
                sqlWhere.Add(" RECEIPT_LINE_ID " + intransitDetailQuery.ReceiptLineID);
            }
            if (!string.IsNullOrEmpty(intransitDetailQuery.OwnerID))
            {
                sqlWhere.Add(" OWNER_ID " + intransitDetailQuery.OwnerID);
            }
            if (!string.IsNullOrEmpty(intransitDetailQuery.SkuID))
            {
                sqlWhere.Add(" SKU_ID " + intransitDetailQuery.SkuID);
            }
            if (!string.IsNullOrEmpty(intransitDetailQuery.UomID))
            {
                sqlWhere.Add(" UOM_ID " + intransitDetailQuery.UomID);
            }
            if (!string.IsNullOrEmpty(intransitDetailQuery.PackID))
            {
                sqlWhere.Add(" PACK_ID " + intransitDetailQuery.PackID);
            }
            if (!string.IsNullOrEmpty(intransitDetailQuery.Qty))
            {
                sqlWhere.Add(" QTY " + intransitDetailQuery.Qty);
            }
            if (!string.IsNullOrEmpty(intransitDetailQuery.ShippedQty))
            {
                sqlWhere.Add(" SHIPPED_QTY  " + intransitDetailQuery.ShippedQty);
            }
            if (!string.IsNullOrEmpty(intransitDetailQuery.ReceivedQty))
            {
                sqlWhere.Add(" RECEIVED_QTY  " + intransitDetailQuery.ReceivedQty);
            }
            if (!string.IsNullOrEmpty(intransitDetailQuery.Status))
            {
                sqlWhere.Add(" STATUS  " + intransitDetailQuery.Status);
            }
            return IntransitDetailDA.GetIntransitDetailByQueryList(sqlWhere, intransitDetailQuery, this.dataBaseID[0]);
        }

        public List<IntransitDetailInfo> GetIntransitDetailInfoListByID(string IntransitID)
        {
            return IntransitDetailDA.GetIntransitDetailListByID(IntransitID, this.dataBaseID[0]);
        }

        public string GetIntransitDetailStatus(IntransitDetailInfo intransitDetailInfo)
        {
            string status = IntransitStatus.New;
            if ((intransitDetailInfo.ShippedQty > 0M) && (intransitDetailInfo.ShippedQty <= intransitDetailInfo.Qty))
            {
                status = IntransitStatus.InTransit;
            }
            if ((intransitDetailInfo.ReceivedQty > 0M) && (intransitDetailInfo.ReceivedQty <= intransitDetailInfo.Qty))
            {
                status = IntransitStatus.InTransit;
            }
            if (intransitDetailInfo.Qty == intransitDetailInfo.ReceivedQty)
            {
                status = IntransitStatus.Completed;
            }
            return status;
        }

        public string InsertIntransitDetail(IntransitDetailInfo intransitDetailInfo)
        {
            string lineID = string.Empty;
            try
            {
                if (intransitDetailInfo == null)
                {
                    return lineID;
                }
                if (IntransitDetailDA.CheckIntransitDetailIDUnique(intransitDetailInfo.IntransitID, intransitDetailInfo.LineID.ToString(), this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F13500_002", this.pubParasInfo.UserLanguage, new List<string> { intransitDetailInfo.IntransitID, intransitDetailInfo.LineID.ToString() });
                }
                intransitDetailInfo.UpdatedBy = intransitDetailInfo.CreatedBy;
                intransitDetailInfo.CreatedDate = DateTime.Now;
                intransitDetailInfo.UpdatedDate = DateTime.Now;
                if (IntransitDetailDA.InsertIntransitDetail(intransitDetailInfo, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<IntransitDetailInfo>(null, intransitDetailInfo, AuditAction.Insert, this.pubParasInfo.UserID, "INTRANSIT_DETAIL", this.dataBaseID[0]);
                    lineID = intransitDetailInfo.LineID.ToString();
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return lineID;
        }

        public string UpdateIntransitDetail(IntransitDetailInfo intransitDetailInfo)
        {
            string LineID = string.Empty;
            try
            {
                IntransitDetailInfo oldIntransitDetailInfo = IntransitDetailDA.GetIntransitDetailByID(intransitDetailInfo.IntransitID, intransitDetailInfo.LineID.ToString(), this.dataBaseID[0]);
                if (IntransitDetailDA.UpdateIntransitDetail(intransitDetailInfo, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<IntransitDetailInfo>(oldIntransitDetailInfo, intransitDetailInfo, AuditAction.Update, this.pubParasInfo.UserID, "INTRANSIT_DETAIL", this.dataBaseID[0]);
                    LineID = intransitDetailInfo.IntransitID + "  , " + intransitDetailInfo.LineID;
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return LineID;
        }
    }
}

